// ClickCommand.java
package org.zjx.command.basic;

import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import lombok.extern.slf4j.Slf4j;
import org.zjx.command.Command;
import org.zjx.command.CommandFactory;
import org.zjx.core.IWindow;
import org.zjx.core.impl.StartWindow;
import org.zjx.state.StateManager;
import org.zjx.util.RobotUtil;

@Slf4j
public class ClickCommand extends Command {
    private final int button, count;

    public ClickCommand(IWindow window, int button, int count) {
        super(window, false,null);
        this.button = button;
        this.count = count;
    }

    public ClickCommand(IWindow window, boolean requiresActiveWindow, int button, int count) {
        super(window, true,null);
        this.button = button;
        this.count = count;
    }

    @Override
    protected void doExecute() throws Exception {
        for (int i = 0; i < count; i++) {
            checkInterruption();
            if (getTargetWindow() instanceof StartWindow) {
                RobotUtil.getInstance().clickMouse(button);
            } else {
                User32.INSTANCE.SendMessage(getTargetWindow().getHwnd(), button == 1?0x0201:0x0204, new WinDef.WPARAM(0), null);
                CommandFactory.waitDirectly(targetWindow, 10);
                User32.INSTANCE.SendMessage(getTargetWindow().getHwnd(),  button == 1?0x0202:0x0205, new WinDef.WPARAM(0), null);
            }
            if (i < count - 1) CommandFactory.waitDirectly(targetWindow, 30);
        }
    }
}